﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace tsgl
{
    class bookClass
    {
        public static DataTable getBibliothecaInfo(string bibliothecaId)
        {
            bibliothecaId = "";
            string str;
            if (bibliothecaId == "")
            {
                str = "select 书目编号,图书名称,作者,ISBN,总藏书数量,待入库数量 from 书目信息 where 待入库数量 > 0";
            }
            else
            {
                str = "select 书目编号,图书名称,作者,ISBN,总藏书量,待入库数量 from 书目信息 where 待入库数量 > 0 and 书目编号 = "+bibliothecaId+"";
            }
            return bookDbClass.ExecuteDataTable(str);
        }

        public static string getBarcodeMax()
        {
            string strSql;
            DataTable dt;
            strSql = "select top 1 图书条码 from 图书信息 order by 图书条码 DESC";
            dt = bookDbClass.ExecuteDataTable(strSql);
            return dt.Rows[0][0].ToString();
        }

        public static DataTable bookAdd(string bookBarcode,string bibliothecaId,string bookState,DateTime bookInDate)
        {
            return bookDbClass.ExecuteDataTable("insert into 图书信息(图书条码,书目编号,图书状态,入库日期) values(@a,@b,@c,@d)", new SqlParameter("a", bookBarcode), new SqlParameter("b", bibliothecaId), new SqlParameter("c",bookState), new SqlParameter("d",bookInDate));
        }

        public static DataTable bookTotalNumAdd(string bibliothecaId,int inNums)
        {
           return bookDbClass.ExecuteDataTable("update 书目信息 set 总藏书量=总藏书量+"+inNums+",现存数量=现存数量+"+inNums+",待入库数量=0 where 书目编号="+bibliothecaId+"");
        }

        public static DataTable createTableTemp()
        {
            DataTable sqlDt;
            sqlDt = new DataTable("bookIn");
            //DataColumn column1 = new DataColumn("图书条码", System.Type.GetType("System.String"));
            sqlDt.PrimaryKey = new DataColumn[] { sqlDt.DataSet.Tables["bookIn"].Columns["图书条码"] };
            DataColumn column2 = new DataColumn("书目编号", System.Type.GetType("System.String"));
            DataColumn column3 = new DataColumn("图书状态", System.Type.GetType("System.String"));
            DataColumn column4 = new DataColumn("入库日期", System.Type.GetType("System.String"));
            //sqlDt.Columns.Add(column1);
            sqlDt.Columns.Add(column2);
            sqlDt.Columns.Add(column3);
            sqlDt.Columns.Add(column4);
            //将图书条码字段定义为数据表的主键
            //DataColumn ColArray;
            //ColArray = sqlDt.Columns["图书条码"];
            //sqlDt.PrimaryKey = new DataColumn[] { sqlDt.DataSet.Tables["bookIn"].Columns["图书条码"] };
            //将uniqueConstraint条件约束套用至 图书条码 字段
            sqlDt.Columns["图书条码"].Unique = true;
            return sqlDt;
        }
    }
}
